package com.woniu.shop.service.impl;

import com.woniu.constant.Constant;
import com.woniu.entity.Result;
import com.woniu.entity.ResultCode;
import com.woniu.shop.entity.dto.ComplaintDTO;
import com.woniu.shop.pojo.Complaint;
import com.woniu.shop.mapper.ComplaintMapper;
import com.woniu.shop.service.IComplaintService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.shop.utils.SMSUtils;
import com.woniu.user.feign.UserFeignClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 * 用户投诉表 服务实现类
 * </p>
 *
 * @author malei
 * @since 2023-02-18
 */
@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, Complaint> implements IComplaintService {

    @Autowired
    private ComplaintMapper complaintMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private UserFeignClient userFeignClient;
    @Override
    public Result addComplaint(ComplaintDTO complaintDTO) {
        Field[] fields = complaintDTO.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String methodName = "get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
            Method method = null;
            try {
                method = complaintDTO.getClass().getMethod(methodName);
                Object value = method.invoke(complaintDTO);
                System.out.println(method.getName()+"=======");
                if (value == null ||value.toString().length() == 0) {
                    return Result.error(ResultCode.ERROR.getCode(), "请检查申请信息是否填写完整");
                }
            } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        Complaint complaint = new Complaint();
        BeanUtils.copyProperties(complaintDTO, complaint);
        complaint.setIsDeleted("0");
        complaint.setComplaintType("0");
        int flag = complaintMapper.insert(complaint);
        if (flag == 1){
            stringRedisTemplate.opsForSet().add(Constant.REDIS_DB_UPLOAD_IMAGE,complaint.getComplaintPicture());
            return Result.ok(ResultCode.SUCCESS.getCode(),"添加成功",flag);
        }
        return Result.error(ResultCode.ERROR.getCode(),"添加失败");
    }

    @Override
    public Result queryComplaintByStatus(String shopID, String status) {
         List<Complaint> complaintList = complaintMapper.queryComplaintByStatus(shopID,status);
         if (complaintList.size() > 0 && complaintList != null) {
             return Result.ok(complaintList);
         }
        return Result.error(ResultCode.RESULE_DATA_NONE.getCode(),ResultCode.RESULE_DATA_NONE.getMessage());
    }

    @Override
    public Result replyComplaint(String id,String message) {
       Boolean flag =  complaintMapper.replyComplaint(id,message);
       if (flag){
           //TODO 拿到用户的手机号
           Complaint complaint = complaintMapper.selectById(id);
           System.out.println("===========" + complaint.getUserId());
           Result<String> result = userFeignClient.shopGetUserById(complaint.getUserId());
           String phone = result.getData();
           System.out.println("===========" + phone);
           String info = "1111";
           try {
               SMSUtils.SendSms(phone,info);
           } catch (ExecutionException | InterruptedException e) {
               throw new RuntimeException(e);
           }
           return Result.ok();
       }
       return Result.error(ResultCode.ERROR.getCode());
    }
}
